﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Text;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Drawing;

/// <summary>
/// Summary description for ClassName
/// </summary>
public class ReportManager
{
    public ReportManager()
    {
        //
        // TODO: Add constructor logic here
        //
    }
    
    public static IList<dynamic> GetData(Dictionary<string,string> c,int type){
          var sql = @"SELECT * FROM Customer c,
                   CustomerCashCard cc 
                   WHERE c.CustomerID = cc.CustomerID ";
      
        StringBuilder sb = new StringBuilder();
        sb.Append(sql);
        
        List<object> param = new List<object>();
        int p=0;
        
        switch(type){
            case 1:
                return GenerateCCMasterListReport(c);
                break;
            case 2:
                return GenerateValidationListReport(c);
                break;
            case 3:
               return GeneratePendingAppListReport(c);
                break;
            case 4:
                return GenerateAppInProgressReport(c);
                break;
      
        }
        
        return new List<dynamic>();
        
    }
    
    public static IList<dynamic> GenerateCCMasterListReport(Dictionary<string,string> c){
         var sql = @"SELECT * FROM Customer c
                   LEFT JOIN CustomerCashCard cc 
                   ON c.CustomerID = cc.CustomerID";
        
        int paramCtr=0;
        string sortCol=string.Empty;
        string sortDir=string.Empty;
        
        StringBuilder sb = new StringBuilder();
        sb.Append(sql);
        Dictionary<string,dynamic> p = new Dictionary<string,dynamic>();
        
        if(c["CustomerID"]!="0" && c["CustomerID"]!="" ){
            p.Add("c.CustomerID",c["CustomerID"]);
            paramCtr++;
        }
        
        if(c["FName"]!=""){
            p.Add("c.FName",c["FName"]);
            paramCtr++;
        }
       
        
        
        if(c["CustomerStatus"]!=""){
            p.Add("c.CustomerStatus",c["CustomerStatus"]);
            paramCtr++;
        }
        
        if(c["CustomerClassification"]!=""){
            p.Add("c.CustomerClassification",c["CustomerClassification"]);
            paramCtr++;
        }
        
        if(c["Region"]!=""){
            p.Add("c.CustomerRegion",c["Region"]);
            paramCtr++;
        }

        if(c["SrcRegion"]!=""){
            p.Add("c.Region",c["SrcRegion"]);
            paramCtr++;
        }

        
        if(c["Solicitor"]!=""){
            p.Add("c.Solicitor",c["Solicitor"]);
            paramCtr++;
        }
        
        if(c["Campaign"]!=""){
            p.Add("c.Campaign",c["Campaign"]);
            paramCtr++;
        }
        
        if(c["Country"]!=""){
            p.Add("c.Country",c["Country"]);
            paramCtr++;
        }
        
        if(c["State"]!=""){
            p.Add("c.CurrMailState",c["State"]);
            paramCtr++;
        }
        
        if(c["RefBy"]!=""){
            p.Add("c.ReferredBy",c["RefBy"]);
            paramCtr++;
        }
        
        if(c["CashCardNo"]!=""){
            p.Add("cc.CashCardNo",c["CashCardNo"]);
            paramCtr++;
        }
        
        if(c["CCStatus"]!=""){
            p.Add("cc.CashCardStatus",c["CCStatus"]);
            paramCtr++;
        }
        
        if(c["CCDelStatus"]!=""){
            p.Add("cc.CardDelStatus",c["CCDelStatus"]);
            paramCtr++;
        }
        
        if(c["DocStatus"]!=""){
            p.Add("cc.DocumentStatus",c["DocStatus"]);
            paramCtr++;
        }
        
        if(c["PartnerBank"]!=""){
            p.Add("cc.PartnerBank",c["PartnerBank"]);
            paramCtr++;
        }
        
        if(c["DelCourier"]!=""){
            p.Add("cc.CardDelCourier",c["DelCourier"]);
            paramCtr++;
        }
        
        if(c["CCSDateFrom"]!=""){
            p.Add("CashCardStatusDate",c["CCSDateFrom"]);
            paramCtr++;
        }
        if(c["DelSDateFrom"]!=""){
            p.Add("DelStatusDate",c["DelSDateFrom"]);
            paramCtr++;
        }
        if(c["DelDateFrom"]!=""){
            p.Add("DelDate",c["DelDateFrom"]);
            paramCtr++;
        }
        if(c["ExpDateFrom"]!=""){
            p.Add("CardExpDate",c["ExpDateFrom"]);
            paramCtr++;
        }
        if(c["sort"]!=""){
            sortCol=c["sort"];
            sortDir=c["sortdir"];
        }
        
       if(paramCtr>0){
             int ctr=0;
             string cond=string.Empty;
             foreach(KeyValuePair<string,dynamic> pair in p){
               if(ctr==0){
                    cond =" WHERE ";
                }else{
                    cond=" AND ";
                }
               
               // cond=" AND "; 
                if(string.Compare(pair.Key,"CashCardStatusDate")==0){
                     sb.Append(cond).Append(string.Format("cc.CashCardStatusDate>='{0}' AND cc.CashCardStatusDate<='{1}'",c["CCSDateFrom"],c["CCSDateTo"]));
                }else if(string.Compare(pair.Key,"DelStatusDate")==0){
                     sb.Append(cond).Append(string.Format(" cc.DelStatusDate>='{0}' AND cc.DelStatusDate<='{1}'",c["DelSDateFrom"],c["DelSDateTo"]));
                }else if(string.Compare(pair.Key,"DelDate")==0){
                     sb.Append(cond).Append(string.Format(" cc.DelDate>='{0}' AND cc.DelDate<='{1}'",c["DelDateFrom"],c["DelDateTo"]));
                }else if(string.Compare(pair.Key,"CardExpDate")==0){
                     sb.Append(cond).Append(string.Format(" cc.CardExpDate>='{0}' AND cc.CardExpDate<='{1}'",c["ExpDateFrom"],c["ExpDateTo"]));
                }else if(string.Compare(pair.Key,"c.FName")==0){
                     sb.Append(cond).Append(string.Format(" ({0} LIKE '{1}%'",pair.Key,pair.Value));
                     sb.Append(" OR ").Append(string.Format(" {0} LIKE '{1}%')","c.LName",pair.Value));
                     //check if there is a space, if yes add the LName condition
                     string[] lname = pair.Value.Split(' ');
                     if(lname.Length>1){
                        foreach(var ln in lname){
                             sb.Append(" OR ").Append(string.Format(" {0} LIKE '{1}%'","c.LName",ln));
                        }
                      
                        
                     }
                     
                }else{
                    sb.Append(cond).Append(string.Format(" {0}='{1}'",pair.Key,pair.Value));
                }
                ctr++;
            }
            
          
            
            
            
        }
    
       //sorting
      if(sortCol!=string.Empty){
            sb.Append(string.Format(" ORDER BY {0} {1}",sortCol,sortDir));
        }
    
        var query = DBManager.Query(sb.ToString(),null);
        return query;
        
    }
    
    public static IList<dynamic> GenerateValidationListReport(Dictionary<string,string> c){
        
         //get Customer and Card status in AppConstants
        string customer_lead = AppConstants.CUSTOMER_LEAD;
        string customer_prospect = AppConstants.CUSTOMER_PROSPECT;
        string customer_customer = AppConstants.CUSTOMER_Customer;
        string card_new = AppConstants.CASHCARD_NEW;
        string card_pending = AppConstants.CASHCARD_PENDING;
        
         /*var sql = @"SELECT *,datediff(DAY,ApplicationDate,GETDATE()) as AppDuration,
                     datediff(DAY,c.DateCreated,GETDATE()) as LeadAging
                     FROM Customer c, CustomerCashCard cc 
                     WHERE c.CustomerID = cc.CustomerID 
                     AND c.CustomerStatus IN ('Lead','Prospect')
                     AND cc.CashCardStatus IN ('new','p')";*/
        
         var sql = string.Format("SELECT *,datediff(DAY,ApplicationDate,GETDATE()) as AppDuration, datediff(DAY,c.DateCreated,GETDATE()) as LeadAging  FROM Customer c LEFT JOIN CustomerCashCard cc  ON c.CustomerID = cc.CustomerID  WHERE (c.CustomerStatus IN ('{0}') OR  (c.CustomerStatus IN ('{1}','{2}') AND cc.CashCardStatus IN ('{3}','{4}'))) ",customer_lead,customer_prospect,customer_customer,card_new,card_pending);
        
        
        int paramCtr=0;
        string sortCol=string.Empty;
        string sortDir=string.Empty;
        StringBuilder sb = new StringBuilder();
        sb.Append(sql);
        Dictionary<string,dynamic> p = new Dictionary<string,dynamic>();
        
        
        
        if(c["Region"]!=""){
            p.Add("c.CustomerRegion",c["Region"]);
            paramCtr++;
        }

         if(c["SrcRegion"]!=""){
            p.Add("c.Region",c["SrcRegion"]);
            paramCtr++;
        }
        
        if(c["Solicitor"]!=""){
            p.Add("c.Solicitor",c["Solicitor"]);
            paramCtr++;
        }
        
        if(c["Campaign"]!=""){
            p.Add("c.Campaign",c["Campaign"]);
            paramCtr++;
        }
        
        if(c["Country"]!=""){
            p.Add("c.Country",c["Country"]);
            paramCtr++;
        }
        
        if(c["State"]!=""){
            p.Add("c.CurrMailState",c["State"]);
            paramCtr++;
        }
        
        
        if(c["WithEmail"]!=""){
            p.Add("c.Email",c["WithEmail"]);
            paramCtr++;
        }
        
        if(c["WithLandLineNo"]!=""){
            p.Add("c.LandLineNo",c["WithLandLineNo"]);
            paramCtr++;
        }
        
        if(c["WithMobileNo"]!=""){
            p.Add("c.MobileNo",c["WithMobileNo"]);
            paramCtr++;
        }
        
        if(c["DocStatus"]!=""){
            p.Add("cc.DocumentStatus",c["DocStatus"]);
            paramCtr++;
        }
        
        if(c["AppDateFrom"]!=""){
            p.Add("ApplicationDate",c["AppDateFrom"]);
            paramCtr++;
        }
        
        if(c["CCSDateFrom"]!=""){
            p.Add("CashCardStatusDate",c["CCSDateFrom"]);
            paramCtr++;
        }
       
         if(c["sort"]!=""){
            sortCol=c["sort"];
            sortDir=c["sortdir"];
        }
        
       if(paramCtr>0){
             int ctr=0;
             string cond=string.Empty;
             foreach(KeyValuePair<string,dynamic> pair in p){
               /* if(ctr==0){
                    cond =" WHERE ";
                }else{
                    cond=" AND ";
                }*/
               
                cond=" AND "; 
                if(string.Compare(pair.Key,"CashCardStatusDate")==0){
                     sb.Append(cond).Append(string.Format("cc.CashCardStatusDate>='{0}' AND cc.CashCardStatusDate<='{1}'",c["CCSDateFrom"],c["CCSDateTo"]));
                }else if(string.Compare(pair.Key,"ApplicationDate")==0){
                     sb.Append(cond).Append(string.Format(" cc.ApplicationDate>='{0}' AND cc.ApplicationDate<='{1}'",c["AppDateFrom"],c["AppDateTo"]));
                }else if(string.Compare(pair.Key,"c.LandLineNo")==0){
                      //check value 0=yes 1=no
                    string ll_cond=string.Empty;
                     if(pair.Value=="0"){
                          ll_cond=string.Format(" c.LandLineNo <>'{0}'","63||");
                     }else{
                        ll_cond=string.Format(" c.LandLineNo ='{0}'","63||");
                    }
                     sb.Append(cond).Append(ll_cond);
           
                }else if(string.Compare(pair.Key,"c.MobileNo")==0){
                     //check value 0=yes 1=no
                    string mn_cond=string.Empty;
                     if(pair.Value=="0"){
                          mn_cond=string.Format(" c.MobileNo <>'{0}'","63||");
                     }else{
                        mn_cond=string.Format(" c.MobileNo ='{0}'","63||");
                    }
                     sb.Append(cond).Append(mn_cond);
                }else if(string.Compare(pair.Key,"c.Email")==0){
                     string email_cond=string.Empty;
                     if(pair.Value=="0"){
                          email_cond=string.Format(" c.Email <>'{0}'","");
                     }else{
                        email_cond=string.Format(" c.Email ='{0}'","");
                    }
                     sb.Append(cond).Append(email_cond);
                }else{
                    sb.Append(cond).Append(string.Format(" {0}='{1}'",pair.Key,pair.Value));
                }
                ctr++;
            }
            
            
            
        }
        if(sortCol!=string.Empty){
            sb.Append(string.Format(" ORDER BY {0} {1}",sortCol,sortDir));
        }
        
        var query = DBManager.Query(sb.ToString(),null);
        return query;
        
    }
    
     public static IList<dynamic> GeneratePendingAppListReport(Dictionary<string,string> c){
           
           string card_cancelled = AppConstants.CASHCARD_CANCELLED;
           string del_ud = AppConstants.CARD_DELIVERY_UNDELIVERED;
           string del_dinc = AppConstants.CARD_DELIVERY_DINC;
           string del_del  = AppConstants.CARD_DELIVERY_DELIVERED;
        
          /* var sql = @"SELECT *,datediff(DAY,ApplicationDate,GETDATE()) as AppDuration
                   FROM Customer c,
                   CustomerCashCard cc 
                   WHERE c.CustomerID = cc.CustomerID 
                   AND cc.CashCardStatus<>'can'
                   AND ((cc.CardDelStatus IN ('ud','dinc')) OR (cc.CardDelStatus IN ('del') AND c.IDNumber IS NULL))";*/
        
         var sql = string.Format("SELECT *,datediff(DAY,ApplicationDate,GETDATE()) as AppDuration FROM Customer c, CustomerCashCard cc WHERE c.CustomerID = cc.CustomerID AND cc.CashCardStatus<>'{0}' AND ((cc.CardDelStatus IN ('{1}','{2}')) OR (cc.CardDelStatus IN ('{3}') AND c.IDNumber IS NULL))",card_cancelled,del_ud,del_dinc,del_del);
                  
                 
        
        int paramCtr=0;
        StringBuilder sb = new StringBuilder();
        sb.Append(sql);
        Dictionary<string,dynamic> p = new Dictionary<string,dynamic>();
        
        
        
        if(c["Region"]!=""){
            p.Add("c.CustomerRegion",c["Region"]);
            paramCtr++;
        }

         if(c["SrcRegion"]!=""){
            p.Add("c.Region",c["SrcRegion"]);
            paramCtr++;
        }
        
        if(c["Solicitor"]!=""){
            p.Add("c.Solicitor",c["Solicitor"]);
            paramCtr++;
        }
        
        if(c["Campaign"]!=""){
            p.Add("c.Campaign",c["Campaign"]);
            paramCtr++;
        }
        
        if(c["Country"]!=""){
            p.Add("c.Country",c["Country"]);
            paramCtr++;
        }
        
        if(c["State"]!=""){
            p.Add("c.CurrMailState",c["State"]);
            paramCtr++;
        }
        
        
       if(c["CCStatus"]!=""){
            p.Add("cc.CashCardStatus",c["CCStatus"]);
            paramCtr++;
        }
    
      if(c["CCDelStatus"]!=""){
            p.Add("cc.CardDelStatus",c["CCDelStatus"]);
            paramCtr++;
        }
        
        if(c["AppDateFrom"]!=""){
            p.Add("ApplicationDate",c["AppDateFrom"]);
            paramCtr++;
        }
        
        if(c["CCSDateFrom"]!=""){
            p.Add("CashCardStatusDate",c["CCSDateFrom"]);
            paramCtr++;
        }
        
        if(c["DelSDateFrom"]!=""){
            p.Add("DelStatusDate",c["DelSDateFrom"]);
            paramCtr++;
        }
       
        
       if(paramCtr>0){
             int ctr=0;
             string cond=string.Empty;
             foreach(KeyValuePair<string,dynamic> pair in p){
               /* if(ctr==0){
                    cond =" WHERE ";
                }else{
                    cond=" AND ";
                }*/
               
                cond=" AND "; 
                if(string.Compare(pair.Key,"CashCardStatusDate")==0){
                     sb.Append(cond).Append(string.Format("cc.CashCardStatusDate>='{0}' AND cc.CashCardStatusDate<='{1}'",c["CCSDateFrom"],c["CCSDateTo"]));
                }else if(string.Compare(pair.Key,"ApplicationDate")==0){
                     sb.Append(cond).Append(string.Format(" cc.ApplicationDate>='{0}' AND cc.ApplicationDate<='{1}'",c["AppDateFrom"],c["AppDateTo"]));
                }else if(string.Compare(pair.Key,"DelStatusDate")==0){
                     sb.Append(cond).Append(string.Format(" cc.DelStatusDate>='{0}' AND cc.DelStatusDate<='{1}'",c["DelSDateFrom"],c["DelSDateTo"]));
                }else{
                    sb.Append(cond).Append(string.Format(" {0}='{1}'",pair.Key,pair.Value));
                }
                ctr++;
            }
            
            
            
        }
        
        var query = DBManager.Query(sb.ToString(),null);
        return query;
        
    }
    
    public static IList<dynamic> GenerateAppInProgressReport(Dictionary<string,string> c){
        string card_active = AppConstants.CASHCARD_ACTIVE;
        string card_inac = AppConstants.CASHCARD_INACTIVE;
        string card_ren = AppConstants.CASHCARD_RENEWED;
        string card_rep = AppConstants.CASHCARD_REPLACED;
        string card_exp = AppConstants.CASHCARD_EXPIRED;
        string card_can = AppConstants.CASHCARD_CANCELLED;
        string card_term = AppConstants.CASHCARD_TERMINATED;
        
        
        /*var sql = @"SELECT *,datediff(DAY,ApplicationDate,GETDATE()) as AppDuration
                   FROM Customer c,
                   CustomerCashCard cc 
                   WHERE c.CustomerID = cc.CustomerID 
                   AND cc.CashCardStatus NOT IN ('ac','inac','ren','rep','exp','can','terminated')";*/
  
        var sql = string.Format("SELECT *,datediff(DAY,ApplicationDate,GETDATE()) as AppDuration FROM Customer c,CustomerCashCard cc WHERE c.CustomerID = cc.CustomerID AND cc.CashCardStatus NOT IN ('{0}','{1}','{2}','{3}','{4}','{5}','{6}')",card_active,card_inac,card_ren,card_rep,card_exp,card_exp,card_can,card_term);
           
        
        int paramCtr=0;
        StringBuilder sb = new StringBuilder();
        sb.Append(sql);
        Dictionary<string,dynamic> p = new Dictionary<string,dynamic>();
        
        
        
        if(c["Region"]!=""){
            p.Add("c.CustomerRegion",c["Region"]);
            paramCtr++;
        }

         if(c["SrcRegion"]!=""){
            p.Add("c.Region",c["SrcRegion"]);
            paramCtr++;
        }
        
        if(c["Solicitor"]!=""){
            p.Add("c.Solicitor",c["Solicitor"]);
            paramCtr++;
        }
        
        if(c["Campaign"]!=""){
            p.Add("c.Campaign",c["Campaign"]);
            paramCtr++;
        }
        
        if(c["Country"]!=""){
            p.Add("c.Country",c["Country"]);
            paramCtr++;
        }
        
        if(c["State"]!=""){
            p.Add("c.CurrMailState",c["State"]);
            paramCtr++;
        }
        
        
       if(c["CCStatus"]!=""){
            p.Add("cc.CashCardStatus",c["CCStatus"]);
            paramCtr++;
        }
    
      if(c["CCDelStatus"]!=""){
            p.Add("cc.CardDelStatus",c["CCDelStatus"]);
            paramCtr++;
        }
    
        if(c["PartnerBank"]!=""){
            p.Add("cc.PartnerBank",c["PartnerBank"]);
            paramCtr++;
        }
        
        if(c["DelCourier"]!=""){
            p.Add("cc.CardDelCourier",c["DelCourier"]);
            paramCtr++;
        }
        
        if(c["AppDateFrom"]!=""){
            p.Add("ApplicationDate",c["AppDateFrom"]);
            paramCtr++;
        }
        
        if(c["CCSDateFrom"]!=""){
            p.Add("CashCardStatusDate",c["CCSDateFrom"]);
            paramCtr++;
        }
        
        if(c["DelSDateFrom"]!=""){
            p.Add("DelStatusDate",c["DelSDateFrom"]);
            paramCtr++;
        }
       
        
       if(paramCtr>0){
             int ctr=0;
             string cond=string.Empty;
             foreach(KeyValuePair<string,dynamic> pair in p){
               /* if(ctr==0){
                    cond =" WHERE ";
                }else{
                    cond=" AND ";
                }*/
               
                cond=" AND "; 
                if(string.Compare(pair.Key,"CashCardStatusDate")==0){
                     sb.Append(cond).Append(string.Format("cc.CashCardStatusDate>='{0}' AND cc.CashCardStatusDate<='{1}'",c["CCSDateFrom"],c["CCSDateTo"]));
                }else if(string.Compare(pair.Key,"ApplicationDate")==0){
                     sb.Append(cond).Append(string.Format(" cc.ApplicationDate>='{0}' AND cc.ApplicationDate<='{1}'",c["AppDateFrom"],c["AppDateTo"]));
                }else if(string.Compare(pair.Key,"DelStatusDate")==0){
                     sb.Append(cond).Append(string.Format(" cc.DelStatusDate>='{0}' AND cc.DelStatusDate<='{1}'",c["DelSDateFrom"],c["DelSDateTo"]));
                }else{
                    sb.Append(cond).Append(string.Format(" {0}='{1}'",pair.Key,pair.Value));
                }
                ctr++;
            }
            
            
            
        }
        
        var query = DBManager.Query(sb.ToString(),null);
        return query;
        
    }
    
    public static PdfPTable GeneratePDF(int reportType,IList<dynamic> reportData){
        try{
            PdfPTable table =null;
            switch(reportType){
                case 1:
                    table = CashCardMasterListPDF(reportData);
                    break;
                case 2:
                    table = ValidationListPDF(reportData);
                    break;
                case 3:
                    table = PendingAppListPDF(reportData);
                    break;
                case 4:
                    table = AppInProgressListPDF(reportData);
                    break;
                case 6:
                    table = AuditReportPDF(reportData);
                    break;
               
            }
            
            return table;
        }catch(Exception e){
            throw e;
        }
        
    }
    
    public static PdfPTable CashCardMasterListPDF(IList<dynamic> reportData){
        PdfPTable table=null;
        try{
            string[]  cols = new []{"Customer Name","Customer Status","Card No","CashCard Status","CashCard Delivery Status","Delivery Date","Partner Bank","Delivery Courier","Region","Solicitor"};
            table =InitializeTable("Cash Card Master List Report",cols);
           
            //add data
            foreach(var data in reportData){
                table.AddCell(FormatPhrase(data.FName.ToString() + " " + data.LName.ToString()));
                table.AddCell(FormatPhrase(data.CustomerStatus.ToString()));
                if(data.CashCardAppNo!=null){
                    table.AddCell(FormatPhrase(data.CashCardAppNo.ToString()));
                }else{
                    table.AddCell(FormatPhrase(string.Empty));
                }
                string ccsd_duration=string.Empty;
                string ccdd_duration=string.Empty;
                
                if(data.CashCardAppNo!=null){
                  ccsd_duration = UtilityManager.GetDuration(data.CashCardStatusDate);
                  ccdd_duration = UtilityManager.GetDuration(data.DelStatusDate);
                  table.AddCell(FormatPhrase(string.Format("{0}({1})",UtilityManager.ShowCCStatus(data.CashCardStatus.ToString()),ccsd_duration)));
                  table.AddCell(FormatPhrase(string.Format("{0}",UtilityManager.ShowDelStatus(data.CardDelStatus.ToString(),data.DelStatusDate))));
                  table.AddCell(FormatPhrase(data.DelDate.ToString("MM-dd-yyyy")));
                  table.AddCell(FormatPhrase(data.PartnerBank.ToString()));
                  table.AddCell(FormatPhrase(data.CardDelCourier.ToString()));
                    
                } else{
                       table.AddCell(FormatPhrase(string.Empty));
                       table.AddCell(FormatPhrase(string.Empty));
                       table.AddCell(FormatPhrase(string.Empty));
                       table.AddCell(FormatPhrase(string.Empty));
                       table.AddCell(FormatPhrase(string.Empty));
                    
                }  
                table.AddCell(FormatPhrase(data.Region.ToString()));
                table.AddCell(FormatPhrase(data.Solicitor.ToString()));
                
            }
            //totals
             table.AddCell(SetTableFooter(cols,"Total Records:" + reportData.Count));
            
            return table;
        }catch(Exception e){
            
            throw e;
        }
        
    }
    
    public static PdfPTable ValidationListPDF(IList<dynamic> reportData){
        PdfPTable table=null;
        try{
           string[] cols = new []{"Customer Name","Customer Status","Region","Solicitor","Campaign","Country","State","Document Status","Lead Aging","Duration(application)","With Email","With Landline","With Mobile"};    
           table = InitializeTable("Validation List Report",cols);
           foreach(var d in reportData){
             table.AddCell(FormatPhrase(d.FName.ToString() + " " + d.LName.ToString()));
             table.AddCell(FormatPhrase(d.CustomerStatus.ToString()));
             table.AddCell(FormatPhrase(d.Region.ToString()));
             table.AddCell(FormatPhrase(d.Solicitor.ToString()));
             table.AddCell(FormatPhrase(d.Campaign.ToString()));
             table.AddCell(FormatPhrase(d.Country.ToString()));
             table.AddCell(FormatPhrase(d.CurrMailState.ToString()));
             if(d.DocumentStatus!=null){
                table.AddCell(FormatPhrase(UtilityManager.ShowDocStatus(d.DocumentStatus.ToString())));
             }else{
                table.AddCell(FormatPhrase(string.Empty));
            }
             
             if(d.LeadAging!=null){
                table.AddCell(FormatPhrase(d.LeadAging.ToString()));
            }else{
                 table.AddCell(FormatPhrase(string.Empty));
            }
            
            if(d.AppDuration!=null){
                table.AddCell(FormatPhrase(d.AppDuration.ToString()));
            }else{
                table.AddCell(FormatPhrase(string.Empty));
                
            }
             table.AddCell(FormatPhrase(d.Email.ToString()));
             table.AddCell(FormatPhrase(UtilityManager.GetPhoneValue(d.LandLineNo.ToString())));
             table.AddCell(FormatPhrase(UtilityManager.GetPhoneValue(d.MobileNo.ToString())));
            
             
          }
           //totals
           table.AddCell(SetTableFooter(cols,"Total Records:" + reportData.Count));
            
          return table;
        }catch(Exception e){
            throw e;
        }
    }
    
    public static PdfPTable PendingAppListPDF(IList<dynamic> reportData){
        PdfPTable table=null;
        try{
           string[] cols = new []{"Customer Name","CashCard No.","CashCard Status","CashCard Delivery Status","Duration (in days)","Region","Solicitor","Country","Partner Bank","Delivery Courier"};    
           table = InitializeTable("Pending Application List Report",cols);
           foreach(var d in reportData){
             table.AddCell(FormatPhrase(d.FName.ToString() + " " + d.LName.ToString()));
             table.AddCell(FormatPhrase(d.CashCardAppNo.ToString()));
             string ccsd_duration = UtilityManager.GetDuration(d.CashCardStatusDate);
             string ccdd_duration = UtilityManager.GetDuration(d.DelStatusDate);
             table.AddCell(FormatPhrase(string.Format("{0}({1})",UtilityManager.ShowCCStatus(d.CashCardStatus.ToString()),ccsd_duration)));
             table.AddCell(FormatPhrase(string.Format("{0}",UtilityManager.ShowDelStatus(d.CardDelStatus.ToString(),d.DelStatusDate))));
             table.AddCell(FormatPhrase(UtilityManager.GetDuration(d.ApplicationDate)));
             table.AddCell(FormatPhrase(d.Region.ToString()));
             table.AddCell(FormatPhrase(d.Solicitor.ToString()));
             table.AddCell(FormatPhrase(d.Country.ToString()));
             table.AddCell(FormatPhrase(d.PartnerBank.ToString()));
             table.AddCell(FormatPhrase(d.CardDelCourier.ToString()));
             
          }
           //totals
             table.AddCell(SetTableFooter(cols,"Total Records:" + reportData.Count));
            
          return table;
        }catch(Exception e){
            throw e;
        }
    }
    
    public static PdfPTable AppInProgressListPDF(IList<dynamic> reportData){
        PdfPTable table=null;
        try{
           string[] cols = new []{"Customer Name","CashCard No.","CashCard Status","CashCard Delivery Status","Duration (in days)","Region","Solicitor","Country","Partner Bank","Delivery Courier"};    
           table = InitializeTable("Card Activation List Report",cols);
           foreach(var d in reportData){
             table.AddCell(FormatPhrase(d.FName.ToString() + " " + d.LName.ToString()));
             table.AddCell(FormatPhrase(d.CashCardAppNo.ToString()));
             string ccsd_duration = UtilityManager.GetDuration(d.CashCardStatusDate);
             string ccdd_duration = UtilityManager.GetDuration(d.DelStatusDate);
             table.AddCell(FormatPhrase(string.Format("{0}({1})",UtilityManager.ShowCCStatus(d.CashCardStatus.ToString()),ccsd_duration)));
             table.AddCell(FormatPhrase(string.Format("{0}",UtilityManager.ShowDelStatus(d.CardDelStatus.ToString(),d.DelStatusDate))));
             table.AddCell(FormatPhrase(UtilityManager.GetDuration(d.ApplicationDate)));
             table.AddCell(FormatPhrase(d.Region.ToString()));
             table.AddCell(FormatPhrase(d.Solicitor.ToString()));
             table.AddCell(FormatPhrase(d.Country.ToString()));
             table.AddCell(FormatPhrase(d.PartnerBank.ToString()));
             table.AddCell(FormatPhrase(d.CardDelCourier.ToString()));
             
          }
          
           //totals
             table.AddCell(SetTableFooter(cols,"Total Records:" + reportData.Count));
            
          return table;
        }catch(Exception e){
            throw e;
        }
    }
    
    public static PdfPTable UndeliveredCardsPDF(IList<dynamic> reportData){
        PdfPTable table=null;
        try{
           string[] cols = new []{"CashCardAppNo","NameOnCard","CashCardNo","CashCardStatus","CardDelStatus","StatusDate"};    
           table = InitializeTable("Undelivered Cards List Report",cols);
           foreach(var d in reportData){
             table.AddCell(FormatPhrase(d.CashCardAppNo.ToString()));
             table.AddCell(FormatPhrase(d.CashCardNo.ToString()));
             table.AddCell(FormatPhrase(d.CashCardStatus.ToString()));
             table.AddCell(FormatPhrase(d.CardDelStatus.ToString()));
             table.AddCell(FormatPhrase(d.StatusDate.ToString()));
             table.AddCell(FormatPhrase(d.NameOnCard.ToString()));
           
             
          }
          
          return table;
        }catch(Exception e){
            throw e;
        }
    }
    
    public static PdfPTable CardMemberStatusPDF(IList<dynamic> reportData){
         PdfPTable table=null;
        try{
           string[] cols = new []{"CashCardAppNo","NameOnCard","CashCardNo","CashCardStatus","CardDelStatus","StatusDate"};    
           table = InitializeTable("Card Member Status Report",cols);
           foreach(var d in reportData){
             table.AddCell(FormatPhrase(d.CashCardAppNo.ToString()));
             table.AddCell(FormatPhrase(d.NameOnCard.ToString()));
             table.AddCell(FormatPhrase(d.CashCardNo.ToString()));
             table.AddCell(FormatPhrase(d.CashCardStatus.ToString()));
             table.AddCell(FormatPhrase(d.CardDelStatus.ToString()));
             table.AddCell(FormatPhrase(d.StatusDate.ToString()));
             
          }
          
          return table;
        }catch(Exception e){
            throw e;
        }
    }
    
     public static PdfPTable AuditReportPDF(IList<dynamic> reportData){
         PdfPTable table=null;
        try{
           string[] cols = new []{"Customer","Cash Card No","FieldChange","TypeOfChange","OldValue","NewValue","ChangeBy","Date Changed"};    
           table = InitializeTable("Audit Report",cols);
           foreach(var d in reportData){
             table.AddCell(FormatPhrase(d.CustomerID.ToString()));
             table.AddCell(FormatPhrase(d.CashCardNo.ToString()));
             table.AddCell(FormatPhrase(d.FieldChange.ToString()));
             table.AddCell(FormatPhrase(d.TypeOfChange.ToString()));
             table.AddCell(FormatPhrase(d.OldValue.ToString()));
             table.AddCell(FormatPhrase(d.NewValue.ToString()));
             table.AddCell(FormatPhrase(d.ChangeBy.ToString()));
             table.AddCell(FormatPhrase(d.DateCreated.ToString()));
            
             
          }
          
          return table;
        }catch(Exception e){
            throw e;
        }
    }
    
    
    
    
    public static PdfPTable InitializeTable(string reportTitle,string[] cols){
            PdfPTable table = new PdfPTable(cols.Length);
            table.DefaultCell.Padding=2;
            table.WidthPercentage=100;
            table.DefaultCell.BorderWidth=1;
            //Color altRow = new Color(242,242,242);
            table.HeaderRows =1;
            //report title
            PdfPCell title = new PdfPCell(new Phrase(reportTitle));
            title.Colspan = cols.Length;
            title.HorizontalAlignment=1; //0=left,1=center.2=right
            table.AddCell(title);
            //add report headers
            foreach(var col in cols){
                table.AddCell(FormatHeaderPhrase(col));
            }    
        return table;
    }
    public static PdfPCell SetTableFooter(string[] cols,string reportTitle){
            //report title
            PdfPCell footer = new PdfPCell(new Phrase(reportTitle));
            footer.Colspan = cols.Length;
            footer.HorizontalAlignment=0; //0=left,1=center.2=right
            
            
        return footer;
    }
    
    
    private static Phrase FormatPhrase(string value)
    {
        return new Phrase(value, FontFactory.GetFont(FontFactory.HELVETICA, 8));
    }
    private static Phrase FormatHeaderPhrase(string value)
    {   var header = new BaseColor(31,73,125);
        return new Phrase(value, FontFactory.GetFont(FontFactory.HELVETICA, 8, iTextSharp.text.Font.BOLD,header ));
    }
    
}
